import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
import store from '@/store';

import Home from "@/pages/Home"
import Login from "@/pages/Login"
import Register from "@/pages/Register"
import Search from "@/pages/Search"
import Detail from '@/pages/Detail';
import AddCartSuccess from '@/pages/AddCartSuccess';
import ShopCart from '@/pages/ShopCart';
import Trade from '@/pages/Trade';
import Pay from '@/pages/Pay';
import PaySuccess from '@/pages/PaySuccess';
import Center from '@/pages/Center';

// 二级路由
import MyOrder from '@/pages/Center/MyOrder';

const routes = [
    // 重定向
    {
        path: '/',
        redirect: '/home'
    },
    // 路由路径
    {
        path: '/home',
        component: Home,
        meta: { show: true }
    },
    {
        path: '/search/:keyword?',
        component: Search,
        name: 'search',
        meta: { show: true }
    },
    {
        path: '/register',
        component: Register,
        meta: { show: false }
    },
    {
        path: '/login',
        component: Login,
        meta: { show: false }
    },
    {
        path: '/detail/:skuid',
        component: Detail,
        meta: { show: true }
    },
    {
        path: '/addcartsuccess',
        component: AddCartSuccess,
        meta: { show: true }
    },
    {
        path: '/shopCart',
        component: ShopCart,
        meta: { show: true }
    },
    {
        path: '/trade',
        component: Trade,
        meta: { show: true },
        beforeEach: (to, from, next) => {
            if (from.path == "/shopcart") {
                next()
            } else {
                next(false)
            }
        }
    },
    {
        path: "/pay",
        component: Pay,
        meta: { show: true },
        // 路由独享守卫
        beforeEach: (to, from, next) => {
            if (from.path == "/trade") {
                next()
            } else {
                next(false)
            }
        }
    },
    {
        path: '/paySuccess',
        component: PaySuccess,
        meta: { show: true }
    },
    {
        path: '/center',
        component: Center,
        meta: { show: true },
        children: [
            {
                path: 'myorder',
                component: MyOrder,
            },
            {
                path: '/center',
                redirect: '/center/myorder'
            }
        ]
    }
]

const router = new VueRouter({
    mode: 'history',
    routes
})

// 全局守卫
router.beforeEach(async (to, from, next) => {
    let token = store.state.user.token
    let name = store.state.user.userInfo.name
    console.log(token);
    // 已登录
    if (token) {
        if (to.path == '/path' || to.path == "/register") {
            next("/home")
        } else {
            // 已经登录但去的不是注册和登录页面
            // 登录了且有用户信息
            if (name) {
                next()
            } else {
                // 登录了且没有用户信息
                try {
                    await store.dispatch("getUserInfo")
                    next
                } catch (error) {
                    // token失效
                    await store.dispatch("userLogout")
                    next("/login")
                }
            }
        }
    } else {
        // 未登录
        let toPath = to.path
        if (toPath.indexOf("/trade") != -1 || toPath.indexOf('/pay') != -1 || toPath.indexOf("/center") != -1) {
            next("/login?redirect=" + toPath)
        } else {
            next()
        }
    }
})

export default router